package br.com.fiap.goaround.parser;

import java.net.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import br.com.fiap.goaround.to.TrafficTO;
 
public class HtmlContentParser {
	
	Collection<TrafficTO> endboa = new ArrayList<TrafficTO>();
	Collection<TrafficTO> endruim = new ArrayList<TrafficTO>();
	
	public HtmlContentParser(){
	}
	
	public void htmlSPReader(String links) throws IOException{
 
		String sourceLine;
		String content = "";
		
		// The URL address of the page to open.
		//URL address = new URL("http://maplink.uol.com.br/v2/corredores/Sao-Paulo-SP.htm");
		URL address = new URL(links.toString());
		// *** new version
		Object urlContent = address.getContent();
 
		// Open the address and create a BufferedReader with the source code.
		// *** old version
		//InputStreamReader pageInput = new InputStreamReader(address.openStream(), "UTF-8");
		InputStreamReader pageInput = new InputStreamReader((InputStream) urlContent, "UTF-8");
		BufferedReader source = new BufferedReader(pageInput);
 
		// Append each new HTML line into one string. Add a tab character.
		while ((sourceLine = source.readLine()) != null)
			content += sourceLine + "\t";
 
		// Remove script tags & inclusive content
		Pattern script = Pattern.compile("<script.*?>.*?</script>");
		Matcher mscript = script.matcher(content);
		while (mscript.find()) content = mscript.replaceAll("");
  
		// Remove the tab characters. Replace with new line characters.
		Pattern nLineChar = Pattern.compile("\t+");
		Matcher mnLine = nLineChar.matcher(content);
		while (mnLine.find()) content = mnLine.replaceAll("\n");
 
		Pattern corredores = Pattern.compile("\\n.*?<table class='corredores'>.*?</table>.*?\\n");
		Matcher mcorredores = corredores.matcher(content);
		
		String corredoresTemp = null;
		while (mcorredores.find()) {
			//for (int i = 0; i < mcorredores.groupCount(); i++) {
			corredoresTemp = mcorredores.group();
			//}
		}
		String[] corredoresFinal = corredoresTemp.split("href='/v2/corredores/Sao-Paulo/");
		
		for (int i = 1; i < corredoresFinal.length; i = i + 2) {

			String regGrava =  (corredoresFinal[i]
			                   .replaceAll(".*?.htm\' ", "+0.00;")
			                   .replaceAll("title=.*?\'>", "+0.00;")
			                   .replaceAll("</a></td><td class=\'.*?<span>", ";")
			                   .replaceAll("</span></td><td class=\'detalhes\'.*", "")
			                   .trim());
			
			if (!regGrava.equals("")){

				//System.out.println(regGrava);
				
				String[] registro = regGrava.split(";");
				
				TrafficTO tse = new TrafficTO();
				tse.setEndereco(registro[2].replaceAll(";", ""));
				tse.setStatus(registro[3].replaceAll(";", ""));
				tse.setDataInclusao(new Date());
				tse.setLongitude(Double.parseDouble(registro[0].replaceAll(";", "").trim()));
				tse.setLatitude(Double.parseDouble(registro[1].replaceAll(";", "").trim()));
				
				if (tse.getStatus().equalsIgnoreCase("livre")){
					endboa.add(tse);
				} else {
					endruim.add(tse);
				}
			}
			
		}
		
		// Print the clean content & close the Readers
		pageInput.close();
		source.close();
	}

	public void htmlStreetReader(String links) throws IOException{
		 
		String sourceLine;
		String content = "";
		
		// The URL address of the page to open.
		//URL address = new URL("http://maplink.uol.com.br/v2/corredores/Sao-Paulo-SP.htm");
		URL address = new URL(links.toString());
		// *** new version
		Object urlContent = address.getContent();
 
		// Open the address and create a BufferedReader with the source code.
		// *** old version
		//InputStreamReader pageInput = new InputStreamReader(address.openStream(), "UTF-8");
		InputStreamReader pageInput = new InputStreamReader((InputStream) urlContent, "UTF-8");
		BufferedReader source = new BufferedReader(pageInput);
 
		// Append each new HTML line into one string. Add a tab character.
		while ((sourceLine = source.readLine()) != null)
			content += sourceLine + "\t";
 
		// Remove script tags & inclusive content
		Pattern script = Pattern.compile("<script.*?>.*?</script>");
		Matcher mscript = script.matcher(content);
		while (mscript.find()) content = mscript.replaceAll("");
  
		// Remove the tab characters. Replace with new line characters.
		Pattern nLineChar = Pattern.compile("\t+");
		Matcher mnLine = nLineChar.matcher(content);
		while (mnLine.find()) content = mnLine.replaceAll("\n");
 
		Pattern corredores = Pattern.compile("\\n.*?<table class='corredores'>.*?</table>.*?\\n");
		Matcher mcorredores = corredores.matcher(content);
		
		String corredoresTemp = null;
		while (mcorredores.find()) {
			//for (int i = 0; i < mcorredores.groupCount(); i++) {
			corredoresTemp = mcorredores.group();
			//}
		}
		String[] corredoresFinal = corredoresTemp.split("href='/v2/Mapa.aspx");
		
		for (int i = 1; i < corredoresFinal.length; i = i + 2) {

			String regGrava =   (corredoresFinal[i]
			                    // longitude
			                    .replaceAll(".*?lng=", "")
			                    // latitude
			                    .replaceAll("&lat=", ";")
			      				// endereco
			                    .replaceAll("\' title.*?\'>", ";")
			      				.replaceAll("</a></td><td class=\'.*?<span>", ";")
			      			    // status do endereco
			      				.replaceAll("</span></td><td class=\'detalhes\'.*", "")
			      				.trim());
			
			if (!regGrava.equals("")){

				//System.out.println(regGrava);
				
				String[] registro = regGrava.split(";");
				
				TrafficTO tse = new TrafficTO();
				tse.setEndereco(registro[2].replaceAll(";", ""));
				tse.setStatus(registro[3].replaceAll(";", ""));
				tse.setDataInclusao(new Date());
				tse.setLongitude(Double.parseDouble(registro[0].replaceAll(";", "").trim()));
				tse.setLatitude(Double.parseDouble(registro[1].replaceAll(";", "").trim()));
				
				if (tse.getStatus().equalsIgnoreCase("Livre")){
					endboa.add(tse);
				} else {
					endruim.add(tse);
				}
			}

		}
		
		// Print the clean content & close the Readers
		pageInput.close();
		source.close();
	}

	public Collection<TrafficTO> getEndboa() {
		return endboa;
	}

	public Collection<TrafficTO> getEndruim() {
		return endruim;
	}
	
}
